package com.example.tool.window;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Color;
import java.awt.Component;

public class HToolWindowRenderer extends DefaultTableCellRenderer {
  public HToolWindowRenderer() {
    center();
  }

  public HToolWindowRenderer(Color color) {
    center();
    setForeground(color);
  }

  /**
   * 设置水平垂直居中
   */
  private void center() {
    setHorizontalAlignment(SwingConstants.CENTER);
    setVerticalAlignment(SwingConstants.CENTER);
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (row >= 0 && column == 0) {
      JCheckBox checkBox = new JCheckBox();
      checkBox.setSelected((boolean) value);
      checkBox.setHorizontalAlignment(SwingConstants.CENTER);
      checkBox.setVerticalAlignment(SwingConstants.CENTER);
      return checkBox;
    }
    if (hasFocus) {
      setBorder(BorderFactory.createEmptyBorder());
    }
    return component;
  }
}